<?php
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_seat.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_seat_status.php";
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_sql_convert.php';


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of api_security
 *
 * @author Thien
 */
class api_seats {
    
    public static function get_all_seat_statuses() {
        $dao_seat_status = new dao_seat_status();
        return $dao_seat_status->get_all();
    }
    
    public static function get_seat_status_by_status_id($seat_status_id) {
        $dao_seat_status = new dao_seat_status();
        return $dao_seat_status->get_by_id($seat_status_id);
    }
    
    public static function get_all_columns($ticket_id) {
        $dao_seat = new dao_seat();
        return $dao_seat->get_all_columns($ticket_id);
    }
    
    public static function get_all_rows($ticket_id) {
        $dao_seat = new dao_seat();
        return $dao_seat->get_all_rows($ticket_id);
    }
    
    public static function get_seat($ticket_id, $row, $column) {
        $dao_seat = new dao_seat();
        return $dao_seat->get_by_position($ticket_id, $row, $column);
    }
    
    public static function get_seat_by_id($seat_id) {
        $dao_seat = new dao_seat();
        return $dao_seat->get_by_id($seat_id);
    }
    
    public static function unchoose_seat($seat_id) {
        $dao_seat = new dao_seat();
        $seat = self::get_seat_by_id($seat_id);
        if ($seat['is_wheelchair']) {
            self::update_seat_status($seat_id, 3);
        } else {
            self::update_seat_status($seat_id, 2);
        }
        
        return $dao_seat->get_by_id($seat_id);
    }
    
    
    
    public static function update_seat_status_to_sold($ticket_id, $row, $column) {
        $dao_seat = new dao_seat();
        $seat = self::get_seat($ticket_id, $row, $column);
        $dao_seat->update_seat_status($seat['id'], 4);
        return $dao_seat->get_by_position($ticket_id, $row, $column);
    }
    
    public static function update_seat_status($seat_id, $seat_status_id) {
        $dao_seat = new dao_seat();
        $seat = $dao_seat->get_by_id($seat_id);
        if ($seat_status_id == 1 || $seat_status_id == 4) {
            api_booking::decrease_available($seat['ticket_id'], 1);
        } else if ($seat_status_id == 2 || $seat_status_id == 3) {
            api_booking::increase_available($seat['ticket_id'], 1);
        }
        $dao_seat->update_seat_status($seat_id, $seat_status_id);
    }
    
}
